package com.intermancer.predictor.select;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.intermancer.predictor.mutation.MutationUtility;
import com.intermancer.predictor.organism.Organism;
import com.intermancer.predictor.store.StoreEntry;
import com.intermancer.predictor.store.StoreEntryDAO;
import com.intermancer.predictor.system.Demiurge;

public class DefaultSelectStrategy implements SelectStrategy {
	
	private static Logger logger = Logger.getLogger(Demiurge.class.getName());
	
	public static final int DEFAULT_NUMBER_OF_ORGANISMS = 2;
	private int numberOfOrganisms;
	
	public DefaultSelectStrategy() {
		this(DEFAULT_NUMBER_OF_ORGANISMS);
	}

	public DefaultSelectStrategy(int numberOfOrganisms) {
		super();
		this.numberOfOrganisms = numberOfOrganisms;
	}

	@Override
	public List<Organism> choose(StoreEntryDAO storeEntryDAO) {
		List<Organism> organisms = new ArrayList<Organism>(numberOfOrganisms);
		int storeSize = storeEntryDAO.getSize();
		if(Level.FINE.equals(logger.getLevel())) {
			logger.fine("--- Selection ---");
		}
		while(organisms.size() < numberOfOrganisms) {
			int index = MutationUtility.getDiceroll(storeSize) - 1;
			StoreEntry entry = storeEntryDAO.getByScoreIndex(index);
			Organism organism = entry.getOrganism();
			if(!organisms.contains(organism )) {
				organisms.add(organism);
				if(Level.FINE.equals(logger.getLevel())) {
					logger.fine(entry.getIdentity().toString());
				}
			}
		}
		return organisms;
	}

	public int getNumberOfOrganisms() {
		return numberOfOrganisms;
	}

	public void setNumberOfOrganisms(int numberOfOrganisms) {
		this.numberOfOrganisms = numberOfOrganisms;
	}

}
